<?php
define('IN_PUBLIC', true);
header('Content-type:text/html; charset=utf-8');

$_GET['db_pass'] = empty($_GET['db_pass']) ? '' : trim($_GET['db_pass']);
if(empty($_GET['db_pass'])){
	$passfile = dirname(__FILE__).'/pass.txt';
	if(file_exists($passfile)){
		$_GET['db_pass'] = trim(file_get_contents($passfile));
	}
}
if(empty($_GET['db_pass'])){
	exit('Database password is empty.');
}

$__self	= dirname($_SERVER ['PHP_SELF']);
$__dir	= str_replace('//', '/', str_replace('\\', '/', $__self).'/');
$__root	= str_replace('\\', '/', dirname(__FILE__)).'/';

$config = file_get_contents($__root.'install/data/config.inc.default');
$config = preg_replace('/##::[\s\S]+?\n/i', '', $config);
if(IN_PUBLIC){
	$config = preg_replace('/##&&\s+/i', '', $config);
	$config = preg_replace('/##\$\$[\s\S]+?\n/i', '', $config);
}else{
	$config = preg_replace('/##&&[\s\S]+?\n/i', '', $config);
	$config = preg_replace('/##\$\$\s+/i', '', $config);
}
$config = preg_replace('/[\r\n]+\s+[\r\n]+/', "\n", $config);
##^^ 安全码
$sitekey = array();
for($i = 0; $i < 5; $i ++){
	$sitekey[] = strtoupper(genRandomString(5));
}
##^^ 前缀
if($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['HTTP_HOST'] == '127.0.0.1'){
	$pre = 'dxe';
}else{
	$hostarr = explode('.', $_SERVER['HTTP_HOST']);
	array_pop($hostarr);
	if($hostarr[0] == 'www'){
		array_shift($hostarr);
	}
	$pre = implode('_', $hostarr);
}
$replace = array(
	'charset'	=> 'utf-8',
	'host'		=> "http://{$_SERVER['HTTP_HOST']}",
	'root'		=> $__root,
	'dir'		=> $__dir,
	#
	'sitekey'	=> implode('-', $sitekey),
	# 数据库配置
	'db_host'	=> 'localhost',
	'db_user'	=> 'root',
	'db_pass'	=> $_GET['db_pass'],
	'db_name'	=> 'dxe_shop',
	'db_pre'	=> 'tb_',
);
foreach($replace as $key => $val){
	$config = preg_replace("/\{@{$key}\}/i", $val, $config);
}
file_put_contents($__root.'data/config.inc.php', $config);
// 加载生成的配置文件
include_once $__root.'data/config.inc.php';

/*******************************************************************************
 * 修改管理员信息
 *******************************************************************************/
$__db = new DBase(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$__db->query("TRUNCATE `tb_manager`");
$oManager = new Manager();
$pass = genRandomString(8);
$pwdmd5 = $oManager->genral_password($pass);
$manager_info = array (
	'username'	=> 'admin',
	'password'	=> $pwdmd5,
	'role'		=> '1',
	'subset'	=> 'DEFAULT',
	'super'		=> 0,
	'debugging'	=> 0,
	'memo'		=> '',
	'email'		=> '',
	'setting'	=> serialize(array(
		'style'		=> 'default',
		'language'	=> 'zh-cn',
		'debug_pr'	=> 0,
		'debug_fb'	=> 0,
	)),
);
$__db->insert("tb_manager", $manager_info);
$oManager->cache_all();

/*******************************************************************************
 * 获取远程配置信息，很重要
 *******************************************************************************/
##^^ License::system_update_notification();

/*******************************************************************************
 * .htaccess
 *******************************************************************************/
$htaccess = file_get_contents($__root.'install/data/htaccess.default');
$htaccess = preg_replace('/\{@webdir\}/i', $__dir, $htaccess);
file_put_contents($__root.'.htaccess', $htaccess);

/*******************************************************************************
 * 删除密码文件
 *******************************************************************************/
$passfile = dirname(__FILE__).'/pass.txt';
if(file_exists($passfile)){
	@unlink($passfile);
}

echo <<<HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>DXE-shop</title>
</head>
<body>

	管理后台帐号：admin<br />
	管理后台密码：{$pass}，
	<a href="./m.php" target="_blank">后台管理中心</a>
</body>
</html>
HTML;

function genRandomString($len){ 
	$chars = array( 
		"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
		"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
		"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
		"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
		"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
		"3", "4", "5", "6", "7", "8", "9" 
	);
	$charsLen = count($chars) - 1;
	shuffle($chars);    // 将数组打乱
	$output = ""; 
	for ($i=0; $i<$len; $i++){ 
		$output .= $chars[mt_rand(0, $charsLen)];
	}
	return $output;
}
?>